/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.log.gui.composite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.annotation.Target;
import java.util.logging.Logger;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.layout.GridData;

import pl.vdl.azbest.annotations.InProgress;
import pl.vdl.azbest.annotations.Level;
import pl.vdl.azbest.annotations.TODO;
import pl.vdl.azbest.annotations.Team;
import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.mremote.Conf;
import pl.vdl.azbest.mremote.cmd.Command;
import pl.vdl.azbest.mremote.cmd.Cmd_saveStringToFile;
import pl.vdl.azbest.mremote.cmd.Cmd_swichLoggingOff;
import pl.vdl.azbest.mremote.cmd.Cmd_swichLogingOn;
import pl.vdl.azbest.mremote.cmd.Invoker;
import pl.vdl.azbest.mremote.gui.GUIFacade;
import pl.vdl.azbest.mremote.gui.SWTElement;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

//<strike></strike>
@InProgress
@TODO(dateAssigned = "21-03-2008", level = Level.IMPORTANT, target = "<br>"
		+ "<strike>1. GUI for system logging and communication </strike>"
		+ "<br>", done = "1", responsible = Team.AZBEST)
public class CompositeSystemConsole extends Composite {

	private Text textArea = null;

	private Logger logger = Logger.getLogger(getClass().getName()); // @jve:decl-index=0:

	{
		LoggingTheGathering.addPath(getClass().getName());
	}
	public CompositeSystemConsole(Composite parent, int style) {
		super(parent, style);
		initialize();
		init();
	}

	private void initialize() {
		textArea = new Text(this, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.heightHint = 200;
		gridData.verticalAlignment = GridData.FILL;
		textArea.setEditable(false);
		textArea.setLayoutData(gridData);
		textArea.setFont(new Font(Display.getDefault(), "Lucida Console", 8,
				SWT.NORMAL));
		textArea
				.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
					public void widgetDisposed(
							org.eclipse.swt.events.DisposeEvent e) {
					}
				});
		this.setLayout(gridLayout);
		createGroup();
		this.setSize(new Point(464, 259));
		this.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
			public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
				Command c = new Cmd_swichLoggingOff();
				Invoker.getInstance().addCommand(c);
				Invoker.getInstance().execute();
				GUIFacade.getInstance().removeComposite(CompositeSystemConsole.this);
			}
		});
	}

	private void saveStringToFile(String filePath, String txt) {
		File f = new File(filePath);
		try {
			PrintWriter pw = new PrintWriter(f);
			pw.println(txt);
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		}

	}

	private void init() {
		Command c = new Cmd_swichLogingOn(this.textArea);
		Invoker.getInstance().addCommand(c);
		Invoker.getInstance().execute();
	}

	private SWTElementInner swtEi = null;

	private Group group = null;

	private ToolBar toolBar1 = null;

	private Button checkBox = null;

	class SWTElementInner extends SWTElement {
		SWTElementInner(Shell s) {
			this.sShell = s;
		}

		@Override
		protected void createSShell() {
		}
	}

	/**
	 * This method initializes group	
	 *
	 */
	private void createGroup() {
		GridData gridData2 = new GridData();
		gridData2.verticalAlignment = GridData.CENTER;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.grabExcessVerticalSpace = false;
		gridData2.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 3;
		group = new Group(this, SWT.NONE);
		createToolBar1();
		group.setLayoutData(gridData2);
		group.setLayout(gridLayout1);
		checkBox = new Button(group, SWT.CHECK);
		checkBox.setText("No class path");
		checkBox.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if(checkBox.isEnabled())LoggingTheGathering.setLoggersToTextAreaConsoleNoClassPath(textArea);
				else LoggingTheGathering.setLoggersToTextAreaConsole(textArea);
			}
		});
	}

	/**
	 * This method initializes toolBar1	
	 *
	 */
	private void createToolBar1() {
		GridData gridData1 = new GridData();
		gridData1.horizontalSpan = 2;
		toolBar1 = new ToolBar(group, SWT.NONE);
		toolBar1.setLayoutData(gridData1);
		ToolItem toolItem = new ToolItem(toolBar1, SWT.PUSH);
		toolItem.setText("Save to file");
		ToolItem toolItem1 = new ToolItem(toolBar1, SWT.PUSH);
		toolItem1.setText("Clear");
		

		toolItem.setText("Save To File");
		toolItem
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						FileDialog fd = new FileDialog(
								CompositeSystemConsole.this.getShell(),
								SWT.SAVE);
						fd.setFilterExtensions(new String[] { ".txt" });
						fd.open();

						String txt = textArea.getText();
						String path = fd.getFilterPath()
								+ Conf.getInstance().PathS + fd.getFileName();

						Command c = new Cmd_saveStringToFile(txt, path);
						Invoker.getInstance().addCommand(c);
						Invoker.getInstance().execute();

					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
	
		toolItem1.setText("Clear");
		toolItem1
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						textArea.setText("");
					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
	
	}
} // @jve:decl-index=0:visual-constraint="60,41"
